//
//  XMLNetWorkTools.swift
//  Weibo
//
//  Created by mac on 16/5/12.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import AFNetworking

enum RequestType :Int {

    case GET = 0
    case POST = 1
}

class XMLNetWorkTools: AFHTTPSessionManager {

    static let sharedTools :XMLNetWorkTools = {
    
        let tools = XMLNetWorkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    func request(requestType :RequestType,url :String,parameters :AnyObject?,callBack :(response :AnyObject?,error :NSError?) -> ()) {
        
        if requestType == .GET {
        
            GET(url, parameters: parameters, progress: nil, success: { (_, response) -> Void in
                
                callBack(response: response, error: nil)
                
                }, failure: { (_, error) -> Void in
                    callBack(response: nil, error: error)
            })
            
        }else {
        
            POST(url, parameters: parameters, progress: nil, success: { (_, response) -> Void in
                
                callBack(response: response, error: nil)
                
                }, failure: { (_, error) -> Void in
                    callBack(response: nil, error: error)
            })
        }
        
        
    }
}
//发微博相关接口
extension XMLNetWorkTools {

    func sendWeiBoMessage(access_token :String,status :String,callBack :(response :AnyObject?,error :NSError?) -> ()) {
    
        let url = "https://api.weibo.com/2/statuses/update.json"
        let params = [
            
            "access_token" : access_token,
            "status" : status
        ]
        
        request(.POST, url: url, parameters: params) { (response, error) -> () in
            callBack(response: response, error: error)
        }
        
        
    }
}

//首页数据获取接口
extension XMLNetWorkTools {
    //增加一个maxID的参数是为了上拉加载数据，设置一个默认值可以防止在外界没有传参数的情况下运行崩溃
    func requestHomeData(access_token :String,maxID :Int64 = 0,sinceID :Int64 = 0,callBack :(response :AnyObject?,error :NSError?) -> ()) {
    
        let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
        let params = [
        
            "access_token" : access_token,
            "max_id" : "\(maxID)",
            "since_id" : "\(sinceID)"
        ]
        print("haha-" + url + "?access_token=\(access_token)")
        request(.GET, url: url, parameters: params) { (response, error) -> () in
            
            callBack(response: response, error: error)
        }
    }
}

extension XMLNetWorkTools {

    func requestAccessToken(code :String,callBack :(response :AnyObject?,error :NSError?) -> ()) {
        
        let url = "https://api.weibo.com/oauth2/access_token"
        let params = [
            
            "client_id" : weiboAppKey,
            "client_secret" : weiboAppSecret,
            "grant_type" : "authorization_code",
            "code" : code,
            "redirect_uri" : weiboRedirect_uri
        ]
        request(.POST, url: url, parameters: params) { (response, error) -> () in
            callBack(response: response, error: error)
        }
    }
    
    func requestUserInfo(userAcount: XMLUserAcount, callBack :(response :AnyObject?,error :NSError?) -> ()) {
        
        let url = "https://api.weibo.com/2/users/show.json"
        let params = [
            
            "access_token" : userAcount.access_token!,
            "uid" : "\(userAcount.uid)"
        ]
        request(.GET, url: url, parameters: params) { (response, error) -> () in
            callBack(response: response, error: error)
        }
        
        
    }
}
